package siruna.WE.installwidgets;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Handler om het inlezen van de het xml bestadn op de server af te handelen,
 * code gevonden op het net, aangepast aan de noden van de applicatie
 * 
 * @author Tim
 * 
 */
public class InstallWidgetsHandler extends DefaultHandler {

	// ===========================================================
	// Fields
	// ===========================================================

	private boolean in_outertag = false;
	private boolean in_innertag = false;
	private boolean in_mytag = false;
	private boolean in_item = false;
	private boolean in_title = false;
	private boolean in_description = false;

	private int i = 0;
	private StringBuffer help;
	private ParsedInstallWidgetsDataSet myParsedExampleDataSet = new ParsedInstallWidgetsDataSet();

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public ParsedInstallWidgetsDataSet getParsedData() {
		return this.myParsedExampleDataSet;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedExampleDataSet = new ParsedInstallWidgetsDataSet();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		help = new StringBuffer();
		if (localName.equals("item")) {
			this.in_item = true;
		} else if (localName.equals("title")) {
			this.in_title = true;
		} else if (localName.equals("description")) {
			this.in_description = true;

		} else if (localName.equals("mytag")) {
			this.in_mytag = true;
		} else if (localName.equals("enclosure")) {
			// Extract an Attribute
			String attrValue = atts.getValue("url");
			myParsedExampleDataSet.addelement(i, "download", attrValue);
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")) {
			this.in_item = false;
			i++;
		} else if (localName.equals("title")) {
			this.in_title = false;
			if (this.in_item) {
				// System.out.println("end title: " + help.toString());
				myParsedExampleDataSet.addelement(i, "title", help.toString());
			}

		} else if (localName.equals("description")) {
			this.in_description = false;
			if (this.in_item) {
				myParsedExampleDataSet.addelement(i, "description", help
						.toString());
			}

		}

		else if (localName.equals("mytag")) {
			this.in_mytag = false;
			// System.out.println("buffer: " + help);
		} else if (localName.equals("tagwithnumber")) {
			// Nothing to do here
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_item) {
			// myParsedExampleDataSet.setExtractedString(new String(ch, start,
			// length));
			help.append(new String(ch, start, length));
		}
	}
}